package com.devilvan.betrice.jpxgmn.controller;

import com.alibaba.fastjson.JSONObject;
import com.devilvan.betrice.common.BetriceResultVo;
import com.devilvan.betrice.exception.BetriceException;
import com.devilvan.betrice.jpxgmn.entity.JpxgmnDownloadEntity;
import com.devilvan.betrice.jpxgmn.po.JpxgmnDetailPo;
import com.devilvan.betrice.jpxgmn.po.JpxgmnHeaderPo;
import com.devilvan.betrice.jpxgmn.service.JpxgmnDetailService;
import com.devilvan.betrice.jpxgmn.service.JpxgmnHeaderService;
import com.devilvan.betrice.jpxgmn.vo.JpxgmnDetailRequestVo;
import com.devilvan.betrice.jpxgmn.vo.JpxgmnDownloadVo;
import com.devilvan.betrice.util.UCollection;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutionException;

/**
 * @author Evad.Wu
 * @Description JPXGMN-头信息表 控制器
 * @date 2022-11-16
 */
@RestController
@RequestMapping(value = "jpxgmnHeaderController")
@Tag(name = "JPXGMN-头信息表 控制器")
public class JpxgmnController {
    @Resource(name = "jpxgmnHeaderServiceImpl")
    private JpxgmnHeaderService jpxgmnHeaderService;
    @Resource(name = "jpxgmnDetailServiceImpl")
    private JpxgmnDetailService jpxgmnDetailService;

    /**
     * 步骤一：先到网站上根据关键字查询，采集该关键字下的所有条目，包含明细网址
     * header 解析报文控制器
     *
     * @param url 请求网址（带参数）
     * @return 形式返回
     */
    @Operation(summary = "步骤一：先到网站上根据关键字查询，采集该关键字下的所有条目，包含明细网址 header 解析报文控制器")
    @GetMapping(value = "doJpxgmnHeaderRequest")
    public BetriceResultVo<JpxgmnHeaderPo> doJpxgmnHeaderRequest(
            @RequestParam String url,
            @RequestParam String keyword,
            @RequestHeader Map<String, String> requestHeader) {
        // 提取请求需要的header
        Map<String, String> subHeader = UCollection.checkSubMap(requestHeader, "cookie");
        BetriceResultVo<JpxgmnHeaderPo> betriceResultVo = null;
        try {
            betriceResultVo = jpxgmnHeaderService.doJpxgmnHeaderRequest(url, keyword, subHeader).get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
        return betriceResultVo;
    }

    /**
     * 步骤二：根据步骤一采集到的明细网址，采集每个明细内的图片
     * detail 解析报文控制器
     *
     * @param request 报文
     * @return 形式返回
     */
    @Operation(summary = "步骤二：根据步骤一采集到的明细网址，采集每个明细内的图片 detail 解析报文控制器")
    @GetMapping(value = "doJpxgmnDetailRequest")
    public BetriceResultVo<JpxgmnDetailPo> doJpxgmnDetailRequest(
            @RequestParam String url,
            @RequestParam(required = false) String keyword,
            @RequestHeader Map<String, String> requestHeader) {
        // 提取请求需要的header
        Map<String, String> subHeader = UCollection.checkSubMap(requestHeader, "cookie");
        BetriceResultVo<JpxgmnDetailPo> betriceResultVo = null;
        try {
            betriceResultVo = jpxgmnDetailService.doJpxgmnDetailRequest(url, keyword, subHeader).get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
        return betriceResultVo;
    }

    /**
     * 步骤二：定时器执行
     * detail 采集报文控制器
     *
     * @return 形式返回
     */
    @Operation(summary = "步骤二：定时器执行")
    @XxlJob(value = "doJpxgmnDetailScheduler")
    @GetMapping(value = "doJpxgmnDetailScheduler")
    public BetriceResultVo<JpxgmnDetailPo> doJpxgmnDetailScheduler() {
        String request = Optional.ofNullable(XxlJobHelper.getJobParam())
                .orElseThrow(() -> new BetriceException("定时器参数不能为空！"));
        JpxgmnDetailRequestVo jpxgmnDetailRequestVo = JSONObject.parseObject(request, JpxgmnDetailRequestVo.class);
        BetriceResultVo<JpxgmnDetailPo> betriceResultVo = doJpxgmnDetailRequest(
                jpxgmnDetailRequestVo.getUrl(), jpxgmnDetailRequestVo.getKeyword(), jpxgmnDetailRequestVo.getRequestHeader());
        return betriceResultVo;
    }

    /**
     * 步骤三：根据步骤二采集的图片地址，进行下载
     * 根据图片下载地址下载 解析报文控制器
     *
     * @param request 请求报文 {@link JpxgmnDownloadVo}
     * @return 形式回调
     */
    @Operation(summary = "步骤三：根据步骤二采集的图片地址，进行下载 根据图片下载地址下载 解析报文控制器")
    @GetMapping(value = "doJpxgmnDetailDownloadRequest")
    public BetriceResultVo<List<JpxgmnDownloadEntity>> doJpxgmnDetailDownloadRequest(@RequestBody String request) {
        JpxgmnDownloadVo jpxgmnDownloadVo = JSONObject.parseObject(request, JpxgmnDownloadVo.class);
        BetriceResultVo<List<JpxgmnDownloadEntity>> betriceResultVo = jpxgmnHeaderService.doJpxgmnDetailDownloadScheduler(jpxgmnDownloadVo);
        return betriceResultVo;
    }


    /**
     * 步骤三：定时器执行
     * 根据图片下载地址下载
     *
     * @param request 请求报文 {@link JpxgmnDownloadVo}
     */
    @Operation(summary = "步骤三：定时器执行 根据图片下载地址下载")
    @XxlJob(value = "doJpxgmnDetailDownloadScheduler")
    public void doJpxgmnDetailDownloadScheduler() {
        String request = Optional.ofNullable(XxlJobHelper.getJobParam())
                .orElseThrow(() -> new BetriceException("定时器参数不能为空！"));
        JpxgmnDownloadVo jpxgmnDownloadVo = JSONObject.parseObject(request, JpxgmnDownloadVo.class);
        jpxgmnHeaderService.doJpxgmnDetailDownloadScheduler(jpxgmnDownloadVo);
    }
}
